import { Injectable, Inject, BadRequestException } from '@nestjs/common';
import {
  CreateUserDto,
  LoginUserDto,
  RegisterUserDto,
} from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { DbService } from 'src/db/db.service';
import { User } from './entities/user.entity';

@Injectable()
export class UserService {
  @Inject(DbService)
  dbService: DbService;
  async register(registerUserDto: RegisterUserDto) {
    const users: User[] = await this.dbService.read();
    const foundUser = users.find(
      item => item.username === registerUserDto.username
    );
    if (foundUser) {
      throw new BadRequestException('该用户已经注册');
    }
    const user = new User();
    user.username = registerUserDto.username;
    user.password = registerUserDto.password;
    users.push(user);

    await this.dbService.write(users);
    return user;
  }
  async login(loginUserDto: LoginUserDto) {
    const users: User[] = await this.dbService.read();
    const foundUser = users.find(
      item => item.username === loginUserDto.username
    );
    if (!foundUser) {
      throw new BadRequestException('该用户不存在');
    }
    if (foundUser.password !== loginUserDto.password) {
      throw new BadRequestException('用户名或密码错误');
    }
    return {
      ...foundUser,
      msg: '登录成功',
    };
  }
  create(createUserDto: CreateUserDto) {
    return {
      message: 'This action adds a new user',
      data: createUserDto,
    };
  }

  findAll() {
    return `This action returns all user`;
  }

  findOne(id: number) {
    return `This action returns a #${id} user`;
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    console.log('updateUserDto: ', updateUserDto);
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }
}
